package com.mxchip.livestarmobile.mobile.ui.contact

import android.app.Application
import android.util.Log
import androidx.lifecycle.*
import com.mxchip.livestarmobile.mobile.Appli
import com.mxchip.livestarmobile.mobile.db.AppDatabase
import com.mxchip.livestarmobile.mobile.db.entity.Contact
import com.mxchip.livestarmobile.mobile.util.WordToPinYinUtils
import com.mxchip.livestarmobile.mobile.state.AppConstant
import com.orhanobut.hawk.Hawk
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch

class ContactViewModel(application: Application) : AndroidViewModel(application) {

    private val appDatabase: AppDatabase = (application as Appli).database

    private val userId: String = Hawk.get(AppConstant.SP_LOGIN_USER_ID)

    private val contactFilterList: MediatorLiveData<List<Contact>> by lazy {
        MediatorLiveData<List<Contact>>().also {
            it.value = emptyList()
        }
    }

    private val contactAll: LiveData<List<Contact>> by lazy {
        Log.e("ContactViewModel", "Thread.currentThread()" + Thread.currentThread().name)
        appDatabase.contactDao().loadAllByUserId(userId)
    }

    private val searchWord: MutableLiveData<String> by lazy {
        MutableLiveData<String>(null)
    }

    private val contactFilterListObserver: Observer<Any> = Observer {

        val keyword = searchWord.value

        if (keyword.isNullOrBlank()) {
            contactFilterList.value = contactAll.value ?: emptyList()
            return@Observer
        }

        val list = contactAll.value?.filter { contact->

            // val pingyin = contact.pys

            /*if (pingyin != null) {
                if (pingyin.contains(keyword)) {
                    return@filter true
                }
            }*/

            val name = contact.fullName
            if (name != null) {
                if (name.contains(keyword)) {
                    return@filter true
                }
                val pinyin = WordToPinYinUtils.getInstance().getSelling(name)
                if (!pinyin.isNullOrBlank()
                        && pinyin.contains(keyword)) {
                    return@filter true
                }
            }
            val number = contact.hxbNumber
            if (number != null) {
                if (number.contains(keyword)) {
                    return@filter true
                }
            }
            return@filter false
        } ?: arrayListOf()

        contactFilterList.value = list
    }


    init {
        contactFilterList.addSource(contactAll, contactFilterListObserver)
        contactFilterList.addSource(searchWord, contactFilterListObserver)
    }

    fun searchWord(s: String?) {
        searchWord.value = s
    }

    fun searchWord(): LiveData<String> {
        return searchWord
    }

    fun contactFilterList(): LiveData<List<Contact>> {
        return contactFilterList
    }

    fun createContact(name: String, number: String) {


        val contact = Contact(userId = userId)
        contact.fullName = name
        contact.hxbNumber = number

        GlobalScope.launch(Dispatchers.Main) {
            appDatabase.contactDao().insertAll(contact)
        }
    }

    fun updateContact(id: String, name: String, number: String) {


        val contact = Contact(userId = userId)
        contact.uid = id.toLong()
        contact.fullName = name
        contact.hxbNumber = number

        GlobalScope.launch(Dispatchers.Main) {
            appDatabase.contactDao().update(contact)
        }

    }


    fun deleteContact(id: String) {

        val contact = Contact(userId = userId)
        contact.uid = id.toLong()

        GlobalScope.launch(Dispatchers.Main) {
            appDatabase.contactDao().delete(contact)
        }
    }

}